package com.ksu.manager.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;

import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.ksu.manager.dao.IItemManagerDAO;
import com.ksu.manager.dao.IProductDAO;
import com.ksu.manager.model.ItemManager;
import com.ksu.manager.model.Product;
import com.ksu.manager.model.pages.Pagination;
import com.ksu.manager.service.IItemManagerService;
import com.ksu.manager.util.MyUtils;

public class ItemManagerServiceImpl implements IItemManagerService {

	private IItemManagerDAO itemManagerDAO;
	
	private IProductDAO productDAO;
	public void setProductDAO(IProductDAO productDAO) {
		this.productDAO = productDAO;
	}
	public IItemManagerDAO getItemManagerDAO() {
		return itemManagerDAO;
	}
	public void setItemManagerDAO(IItemManagerDAO itemManagerDAO) {
		this.itemManagerDAO = itemManagerDAO;
	}

	public Map<String,Object> queryList(ItemManager itemManager,Pagination page) {
		
		return itemManagerDAO.queryList(itemManager,page);
	}
	
	public void add(ItemManager itemManager) {
		itemManager.setStatTime(new Date());
		itemManagerDAO.create(itemManager);
		Product product = new Product();
		product.setId(itemManager.getProductId());
        product = (Product)productDAO.queryList(product).get(0);
        product.setCurrentTotalNumber(product.getCurrentTotalNumber()+itemManager.getNumber());
        product.setStatTime(new Date());
        productDAO.update(product);
	}

	public void del(ItemManager itemManager) {
		Product product = new Product();
		product.setId(itemManager.getProductId());
        product = (Product)productDAO.queryList(product).get(0);
        product.setCurrentTotalNumber(product.getCurrentTotalNumber()-itemManager.getNumber());
        product.setStatTime(new Date());
        itemManager.setStatTime(new Date());
        itemManager.setStat("0");
		itemManagerDAO.update(itemManager);
		productDAO.update(product);
	}
	@Override
	public void loadExcel(File excelFile) throws FileNotFoundException {
		FileInputStream fin = new FileInputStream(excelFile);
		try {
			// 创建对Excel工作簿文件的引用
			HSSFWorkbook workbook = new HSSFWorkbook(fin);
            ItemManager itemManager = new ItemManager();
			for (int numSheets = 0; numSheets < workbook.getNumberOfSheets(); numSheets++) {
				if (null != workbook.getSheetAt(numSheets)) {
					HSSFSheet aSheet = workbook.getSheetAt(numSheets);//获得一个sheet
					for (int rowNumOfSheet = 0; rowNumOfSheet <= aSheet.getLastRowNum(); rowNumOfSheet++) {
						if (null != aSheet.getRow(rowNumOfSheet)) {
							//读行
							HSSFRow aRow = aSheet.getRow(rowNumOfSheet);
							Product product = new Product();
							
							for (short cellNumOfRow = 0; cellNumOfRow <= aRow.getLastCellNum(); cellNumOfRow++) {
								if (null != aRow.getCell(cellNumOfRow)) {
									//读一行的单元格
									HSSFCell aCell = aRow.getCell(cellNumOfRow);
									int cellType = aCell.getCellType();
									//读取第一行内容
                                    if(rowNumOfSheet==0){
                                    	if(cellNumOfRow==1){
                                    	    itemManager.setProject(getCellValue(cellType,aCell));
                                    	}
                                    	if(cellNumOfRow==3){
                                    	    itemManager.setStaff(getCellValue(cellType,aCell));
                                    	}
                                    	if(cellNumOfRow==5){
                                    	    itemManager.setStatTime(MyUtils.StringToDate(getCellValue(cellType,aCell)));
                                    	}
                                    }else if(rowNumOfSheet >= 2){//读取第三行内容
                                    	if(cellNumOfRow==0){
                                    	    product.setNo(getCellValue(cellType,aCell));
                                    	    product = productDAO.getProduct(product);
                                    	    itemManager.setProductId(product.getId());
                                    	    itemManager.setTypeItemId(product.getTypeItemId());  
                                    	}
                                    	if(cellNumOfRow==1){
                                    		int number = MyUtils.StringToInt(getCellValue(cellType,aCell));
                                    		itemManager.setNumber(number);
                                    		if(number > 0){
                                    			itemManager.setOperation("in");
                                    		}else{
                                    			itemManager.setOperation("out");
                                    		}
                                    	}
                                        if(cellNumOfRow==2){
                                        	itemManager.setNote(getCellValue(cellType,aCell));
                                    	}
                                       
                                    }
                                    
								}
							}
							itemManager.setStat("1");
                        	itemManagerDAO.create(itemManager);
                        	product.setCurrentTotalNumber(product.getCurrentTotalNumber()+itemManager.getNumber());
                        	product.setStatTime(new Date());
                            productDAO.update(product);
							
						}
					}
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				fin.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}
	
	private String getCellValue(int cellType, HSSFCell aCell) {
		String cellValue = null;
		switch (cellType) {
		case HSSFCell.CELL_TYPE_NUMERIC:
			if (HSSFDateUtil.isCellDateFormatted(aCell)) {
				double d = aCell.getNumericCellValue();  
				Date date = HSSFDateUtil.getJavaDate(d);  
				cellValue = MyUtils.dateToString(date);
			}else{
				cellValue = String.valueOf((int) aCell.getNumericCellValue());
			}
			break;
		case HSSFCell.CELL_TYPE_STRING:
			cellValue = aCell.getStringCellValue();
			break;
		case HSSFCell.CELL_TYPE_FORMULA:
			cellValue = String.valueOf(aCell.getNumericCellValue());
			break;
		case HSSFCell.CELL_TYPE_BOOLEAN:
			cellValue = String.valueOf(aCell.getBooleanCellValue());
			break;
		case HSSFCell.CELL_TYPE_BLANK:
			cellValue = null;
			break;
		}

		return cellValue;
	}
	

}
